<template>
  <Form
    class="w-1/3 mx-auto mt-70 shadow-2xl shadow-blue-300 rounded-box"
    @submit="onSubmit"
    :validation-schema="schema"
  >
    <h1 class="text-4xl text-center">Login</h1>
    <div class="w-3/4 mx-auto">
      <label class="input input-bordered flex gap-2 my-4 w-3/4 mx-auto">
        <svg
          xmlns="http://www.w3.org/2000/svg"
          viewBox="0 0 16 16"
          fill="currentColor"
          class="h-4 w-4 opacity-70"
        >
          <path
            d="M2.5 3A1.5 1.5 0 0 0 1 4.5v.793c.026.009.051.02.076.032L7.674 8.51c.206.1.446.1.652 0l6.598-3.185A.755.755 0 0 1 15 5.293V4.5A1.5 1.5 0 0 0 13.5 3h-11Z"
          />
          <path
            d="M15 6.954 8.978 9.86a2.25 2.25 0 0 1-1.956 0L1 6.954V11.5A1.5 1.5 0 0 0 2.5 13h11a1.5 1.5 0 0 0 1.5-1.5V6.954Z"
          />
        </svg>
        <Field
          name="email"
          type="text"
          class="grow"
          placeholder="Email"
          v-model="email"
        />
        <ErrorMessage name="email" class="text-red-500" />
      </label>

      <label class="input input-bordered flex gap-2 my-4 w-3/4 mx-auto">
        <svg
          xmlns="http://www.w3.org/2000/svg"
          viewBox="0 0 16 16"
          fill="currentColor"
          class="h-4 w-4 opacity-70"
        >
          <path
            fill-rule="evenodd"
            d="M14 6a4 4 0 0 1-4.899 3.899l-1.955 1.955a.5.5 0 0 1-.353.146H5v1.5a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1-.5-.5v-2.293a.5.5 0 0 1 .146-.353l3.955-3.955A4 4 0 1 1 14 6Zm-4-2a.75.75 0 0 0 0 1.5.5.5 0 0 1 .5.5.75.75 0 0 0 1.5 0 2 2 0 0 0-2-2Z"
            clip-rule="evenodd"
          />
        </svg>
        <Field
          name="password"
          type="password"
          class="grow"
          placeholder="Password"
          v-model="password"
        />
        <ErrorMessage name="password" class="text-red-500" />
      </label>
      <!-- remember me  and  forget password -->
      <div class="grid grid-cols-2 gap-1">
        <label class="mx-auto">
          <input
            type="checkbox"
            checked="checked"
            class="checkbox checkbox-primary"
          />
          <span class="mx-2">Remember me</span>
        </label>
        <button class="btn btn-link">Forget password?</button>
      </div>
    </div>
    <!-- button -->
    <div class="text-center">
      <button class="btn btn-primary mx-2 my-4" :disabled="isLogging">
        Login
      </button>
      <button
        class="btn btn-secondary mx-2 my-4"
        @click="router.push({ name: 'signup' })"
        :disabled="isLogging"
      >
        Signup
      </button>
    </div>
  </Form>
</template>

<script setup>
import { useQueryClient, useQuery, useMutation } from "@tanstack/vue-query";

import { login as loginApi } from "@/services/ApiAuth";
import { ref } from "vue";
import { useRouter } from "vue-router";
import { useToast } from "vue-toastification";

import { Field, Form, ErrorMessage } from "vee-validate";
import * as yup from "yup";

const toast = useToast();

const schema = yup.object({
  email: yup.string().required().email(),
  password: yup.string().required().min(6),
});

const email = ref("");
const password = ref("");
const router = useRouter();

const { mutate: login, isPending: isLogging } = useMutation({
  mutationFn: ({ email, password }) => loginApi(email, password), //在原始api函数还是接收两个参数,所以我们需要解构出来
  onSuccess: () => {
    // Invalidate and refetch
    toast.success("Login successful");
    router.push("/");
  },
  onError: (error) => {
    toast.error("Login failed: " + error.message);
  },
});

async function onSubmit() {
  login({ email: email.value, password: password.value }); //封装后的函数只接收一个对象参数
}
</script>
